package com.chushouya.product.task;

import com.chushouya.product.service.sync.SuhuishouSyncService;
import com.general.framework.job.starter.TaskUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 速回收同步任务
 * 用于定时同步速回收的品牌和产品数据
 */
@Component
@Slf4j
public class SuhuishouSyncTask {

    @Resource(name = "productSuhuishouSyncService")
    private SuhuishouSyncService suhuishouSyncService;

    /**
     * 同步所有分类的品牌任务
     * 参数格式: 无参数或空
     * 示例: 直接执行即可
     * 
     * 功能: 遍历所有分类，同步速回收的品牌数据到本地
     */
    @XxlJob(value = "suhuishouSyncAllBrandsJobHandler")
    public ReturnT<String> suhuishouSyncAllBrandsJobHandler() {
        final String jobName = "速回收同步所有品牌";
        String param = XxlJobHelper.getJobParam();
        final Long startTime = TaskUtil.taskStart(jobName, param);

        try {
            log.info("开始同步所有分类的品牌");
            
            // 同步所有品牌
            suhuishouSyncService.syncBrandList();
            
            log.info("同步所有分类的品牌完成");
            TaskUtil.taskEnd(jobName, param, startTime);
            return ReturnT.SUCCESS;

        } catch (Exception e) {
            log.error("同步所有品牌任务执行失败", e);
            TaskUtil.taskEnd(jobName, param, startTime);
            return ReturnT.FAIL;
        }
    }

    /**
     * 同步指定分类的品牌任务
     * 参数格式: categoryId
     * 示例: 1
     * 
     * categoryId: 分类ID (必填)
     * 
     * 功能: 同步指定分类下的所有品牌
     */
    @XxlJob(value = "suhuishouSyncBrandJobHandler")
    public ReturnT<String> suhuishouSyncBrandJobHandler() {
        final String jobName = "速回收同步指定分类品牌";
        String param = XxlJobHelper.getJobParam();
        final Long startTime = TaskUtil.taskStart(jobName, param);

        try {
            SyncBrandParam syncParam = new SyncBrandParam(param).invoke();
            
            if (syncParam.getCategoryId() == null) {
                log.error("参数错误: categoryId不能为空");
                TaskUtil.taskEnd(jobName, param, startTime);
                return ReturnT.FAIL;
            }

            log.info("开始同步分类{}的品牌", syncParam.getCategoryId());
            
            // 同步指定分类的品牌
            suhuishouSyncService.syncBrand(syncParam.getCategoryId());
            
            log.info("同步分类{}的品牌完成", syncParam.getCategoryId());
            TaskUtil.taskEnd(jobName, param, startTime);
            return ReturnT.SUCCESS;

        } catch (Exception e) {
            log.error("同步指定分类品牌任务执行失败", e);
            TaskUtil.taskEnd(jobName, param, startTime);
            return ReturnT.FAIL;
        }
    }

    /**
     * 同步所有品牌的产品任务
     * 参数格式: 无参数或空
     * 示例: 直接执行即可
     * 
     * 功能: 遍历所有分类和品牌，同步速回收的产品数据到本地
     */
    @XxlJob(value = "suhuishouSyncAllProductsJobHandler")
    public ReturnT<String> suhuishouSyncAllProductsJobHandler() {
        final String jobName = "速回收同步所有产品";
        String param = XxlJobHelper.getJobParam();
        final Long startTime = TaskUtil.taskStart(jobName, param);

        try {
            log.info("开始同步所有品牌的产品");
            
            // 同步所有产品
            suhuishouSyncService.syncProductList();
            
            log.info("同步所有品牌的产品完成");
            TaskUtil.taskEnd(jobName, param, startTime);
            return ReturnT.SUCCESS;

        } catch (Exception e) {
            log.error("同步所有产品任务执行失败", e);
            TaskUtil.taskEnd(jobName, param, startTime);
            return ReturnT.FAIL;
        }
    }

    /**
     * 同步指定分类和品牌的产品任务
     * 参数格式: categoryId,brandId
     * 示例: 1,2
     * 
     * categoryId: 分类ID (必填)
     * brandId: 品牌ID (必填)
     * 
     * 功能: 同步指定分类和品牌下的所有产品
     */
    @XxlJob(value = "suhuishouSyncProductJobHandler")
    public ReturnT<String> suhuishouSyncProductJobHandler() {
        final String jobName = "速回收同步指定产品";
        String param = XxlJobHelper.getJobParam();
        final Long startTime = TaskUtil.taskStart(jobName, param);

        try {
            SyncProductParam syncParam = new SyncProductParam(param).invoke();
            
            if (syncParam.getCategoryId() == null || syncParam.getBrandId() == null) {
                log.error("参数错误: categoryId和brandId不能为空");
                TaskUtil.taskEnd(jobName, param, startTime);
                return ReturnT.FAIL;
            }

            log.info("开始同步分类{}品牌{}的产品", syncParam.getCategoryId(), syncParam.getBrandId());
            
            // 同步指定分类和品牌的产品
            suhuishouSyncService.syncProduct(syncParam.getCategoryId(), syncParam.getBrandId());
            
            log.info("同步分类{}品牌{}的产品完成", syncParam.getCategoryId(), syncParam.getBrandId());
            TaskUtil.taskEnd(jobName, param, startTime);
            return ReturnT.SUCCESS;

        } catch (Exception e) {
            log.error("同步指定产品任务执行失败", e);
            TaskUtil.taskEnd(jobName, param, startTime);
            return ReturnT.FAIL;
        }
    }

    /**
     * 品牌同步参数类
     */
    @Data
    private class SyncBrandParam {
        private String param;
        private Long categoryId;

        private SyncBrandParam(String param) {
            this.param = param;
        }

        private SyncBrandParam invoke() {
            categoryId = null;

            // xxl-job 手动调用传参
            // 参数格式: categoryId
            if (Strings.isNotBlank(param)) {
                categoryId = TaskUtil.getParamId(param.trim());
            }
            
            return this;
        }
    }

    /**
     * 产品同步参数类
     */
    @Data
    private class SyncProductParam {
        private String param;
        private Long categoryId;
        private Long brandId;

        private SyncProductParam(String param) {
            this.param = param;
        }

        private SyncProductParam invoke() {
            categoryId = null;
            brandId = null;

            // xxl-job 手动调用传参
            // 参数格式: categoryId,brandId
            if (Strings.isNotBlank(param)) {
                String[] params = param.split(",", -1);
                
                if (params.length >= 1 && Strings.isNotBlank(params[0])) {
                    categoryId = TaskUtil.getParamId(params[0].trim());
                }
                
                if (params.length >= 2 && Strings.isNotBlank(params[1])) {
                    brandId = TaskUtil.getParamId(params[1].trim());
                }
            }
            
            return this;
        }
    }
}
